package com.routz.nettydemo.customprotocol.server;

import com.routz.nettydemo.customprotocol.entity.BraceletData;
import com.routz.nettydemo.customprotocol.entity.BraceletDataPacket;
import com.routz.nettydemo.customprotocol.entity.ChickenProtocol;
import com.routz.nettydemo.customprotocol.entity.DeviceInfo;
import com.routz.nettydemo.customprotocol.entity.GPRS;
import com.routz.nettydemo.customprotocol.entity.Location;
import com.routz.nettydemo.customprotocol.entity.PacketHead;
import com.routz.nettydemo.customprotocol.entity.PacketTail;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.TreeSet;

public class ChickenServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("---------- begin ---------");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ChickenProtocol protocol = (ChickenProtocol) msg;
        DeviceInfo deviceInfo = protocol.getDeviceInfo();
        System.out.println("---- 设备信息 ----");
        System.out.println("---- GPRS ----");
        GPRS gprs = deviceInfo.getGprs();
        Location location = gprs.getLocation();
        System.out.println(
                "---- GPRS: " +
                gprs.getLocStatus() + ", " +
                location.getLatitude() + ", " +
                location.getLatitudeHemisphere() + ", " +
                location.getLongitude() + ", " +
                location.getLongitudeHemisphere()+ ", " +
                gprs.getSim() +
                "----");

        System.out.println("---- 脚环信息 ----");
        TreeSet<BraceletDataPacket> braceletDataPackets = protocol.getBraceletDataPackets();
        for (BraceletDataPacket braceletDataPacket : braceletDataPackets) {
            PacketHead packetHead = braceletDataPacket.getPacketHead();
            System.out.println("包数量:" + packetHead.getPacketSize() + " 包编号" + packetHead.getPacketNum());
            BraceletData[] braceletDatas = braceletDataPacket.getBraceletDatas();
            PacketTail packetTail = braceletDataPacket.getPacketTail();
            System.out.println("---- 包头信息 ----");
            System.out.println("---- 脚环数据 ----");
            for (BraceletData braceletData : braceletDatas) {
                System.out.println(
                        "编号:" + braceletData.getBraceletNum() +
                                " 步数:" + braceletData.getStep() +
                                " 温度:" + braceletData.getTemperature() +
                                " 电压:" + braceletData.getVoltage() +
                                " 距离:" + braceletData.getDistance());
            }
            System.out.println("---- 包尾信息 ----");
            System.out.println("卡号末尾:" + packetTail.getCarNumLast() + " " + "脚环数量:" + packetTail.getBraceletSize());
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
        System.out.println("---------- end ---------");
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
